草庐IT

C++ operator+ 和 operator+= 重载

全部标签

java - 我可以在 Java 中重载变量吗?

我正在编写一个类来表示矩阵。我希望它看起来像这样:publicclassmatrix{privateint[][]matrix;privatedouble[][]matrix;//Andsoonandsoforthsothattheusercanenteranyprimitivetypeand//getamatrixofit}这是合法的代码,还是我必须根据其矩阵包含的数据类型使用不同的变量名称? 最佳答案 你不能重载变量。使用您的方法,您应该给它们不同的名称,然后重载getMatrix不同类型的方法。更好的方法是使用Java泛型:p

java - 方法重载显示 'incompatible type error' 带 float 但不带 double

我试过这个方法重载代码,但我得到了错误nosuitablemethodfoundforadd(double,double)代码:classAdder{staticfloatadd(floata,floatb){returna+b;}staticintadd(inta,intb){returna+b;}}classTestOverloading1{publicstaticvoidmain(String[]args){System.out.println(Adder.add(11.5,11.5));System.out.println(Adder.add(27,21));}}在编写时,参数

Java 流 : use optional filter() operations on chaining

注意:此问题与java.util.Optional不相关。在处理流时,我经常使用这样的逻辑:Streamstream=myInitialStream();if(needsFilter1)stream=stream.filter(c->whatever1());if(needsFilter2)stream=stream.filter(c->whatever2());...returnstream.collect(toList());我想要实现的是使用链接将上面的代码转换为单个表达式。我发现这更具可读性和直接性。到目前为止,我发现实现这一目标的唯一方法是:returnmyInitialSt

java - 在 Java 中使用重载方法键入顺序

给定Java中同一个类的两个方法:publicvoiddoSomething(Personperson);publicvoiddoSomething(Employeeemployee);在哪里EmployeeextendsPerson如果我调用:doSomething(employee)我发现doSomething(Person)被调用了。我希望调用具有最接近匹配契约的重载,而不是最抽象的(这是我正在发现的)谁能解释一下为什么? 最佳答案 使用最具体的适用重载-但该重载是在编译时根据employee变量的编译时类型确定的。换句话说:

Java 7 使用可变参数重载

这个问题在这里已经有了答案:关闭9年前。PossibleDuplicate:bugwithvarargsandoverloading?谁能给我解释一下这个是如何工作的:classVararg{staticvoidvararg(int...x){System.out.println("Integer...");}staticvoidvararg(long...x){System.out.println("long...");}publicstaticvoidmain(String[]args){ints=0;vararg(s,s);}}获取编译时错误classVararg{staticv

java - 子类中的方法可以重载父类(super class)中的方法吗?

Java代码:classP{publicvoidhello(){}}classCextendsP{publicvoidhello(Strings){}}我的问题是:类C中的hello是否重载了父类(superclass)P中的同名对象?我的friend说他们不是因为他们不在同一个类(class)。 最佳答案 采用更正式的方法,Java7的Java语言规范指出:Iftwomethodsofaclass(whetherbothdeclaredinthesameclass,orbothinheritedbyaclass,oronedecl

java - 在 Java 中重载方法时的奇怪行为

我今天遇到了这种奇怪的(在我看来)行为。采用这个简单的测试类:publicclassTest{publicstaticvoidmain(String[]args){Testt=newTest();t.run();}privatevoidrun(){Listlist=newArrayList();list.add(newObject());list.add(newObject());method(list);}publicvoidmethod(Objecto){System.out.println("Object");}publicvoidmethod(Listo){System.out

java - NetBeans : diamond operator is not supported in -source 1. 5(使用 -source 7 或更高版本启用菱形运算符)

我写代码的时候不知道为什么:Listdata=newArrayList();是这样说的diamondoperatorisnotsupportedin-source1.5(use-source7orhighertoenablediamondoperator)----(Alt-Entershowshints)我已经在使用JDK1.7。当我在eclipse中打开它时,我没有得到那个错误。 最佳答案 -source1.5表示您的代码将与Java1.5版兼容,并且不能使用稍后引入的语言结构。阅读http://docs.oracle.com/j

java - 在 Java 的重载方法中使用 null

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:MethodOverloadingforNULLparameter以下代码编译正常。publicclassMain{publicvoidtemp(Objecto){System.out.println("ThemethodwiththereceivingparameteroftypeObjecthasbeeninvoked.");}publicvoidtemp(Strings){System.out.println("ThemethodwiththereceivingparameteroftypeString

Java 不能重载任何运算符。为什么?

这个问题在这里已经有了答案:关闭12年前。PossibleDuplicate:Javaoperatoroverload在c++中,我们可以进行运算符重载。但Java也是一种面向对象的语言。那么为什么java不支持重载呢?